package com.example.demo.BookService.impl.impl;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.demo.BookService.impl.LendRecordService;
import com.example.demo.entity.LendRecord;
import com.example.demo.mapper.LendRecordMapper;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class LendRecordServiceImpl implements LendRecordService {

    @Resource
    LendRecordMapper lendRecordMapper;

    /**
     * 根据图书编号删除
     * @param isbn
     */
    public void deleteByIsbn(String isbn) {
        Map<String, Object> map = new HashMap<>();
        map.put("isbn", isbn);
        lendRecordMapper.deleteByMap(map);
    }

    /**
     * 删除一条借阅记录
     * @param lendRecord
     */
    public void deleteRecord(LendRecord lendRecord) {
        Map<String, Object> map = new HashMap<>();
        map.put("isbn", lendRecord.getIsbn());
        map.put("borrownum", lendRecord.getBorrownum());
        lendRecordMapper.deleteByMap(map);
    }

    /**
     * 批量删除借阅记录
     * @param lendRecords
     */
    public void deleteRecords(List<LendRecord> lendRecords) {
        int len = lendRecords.size();// 获取要删除的数据数量
        // 遍历删除每一条数据
        for (int i = 0; i < len; i++) {
            LendRecord curRecord = lendRecords.get(i);
            Map<String, Object> map = new HashMap<>();
            map.put("isbn", curRecord.getIsbn());
            map.put("borrownum", curRecord.getBorrownum());
            lendRecordMapper.deleteByMap(map);
        }
    }

    /**
     * 增加一条借阅记录
     * @param lendRecord
     */
    public void insert(LendRecord lendRecord) {
        lendRecordMapper.insert(lendRecord);
    }

    /**
     * 分页查询借阅记录
     * @param pageNum
     * @param pageSize
     * @param search1
     * @param search2
     * @param search3
     * @return
     */
    public Page<LendRecord> findPage(Integer pageNum, Integer pageSize, String search1, String search2, String search3) {
        LambdaQueryWrapper<LendRecord> wrappers = Wrappers.<LendRecord>lambdaQuery();
        if (StringUtils.isNotBlank(search1)) {
            wrappers.like(LendRecord::getIsbn, search1);
        }
        if (StringUtils.isNotBlank(search2)) {
            wrappers.like(LendRecord::getBookname, search2);
        }
        if (StringUtils.isNotBlank(search3)) {
            wrappers.like(LendRecord::getReaderId, search3);
        }
        return lendRecordMapper.selectPage(new Page<>(pageNum, pageSize), wrappers);
    }

    /**
     * 根据图书编号更新借阅记录
     * @param isbn
     * @param lendRecord
     */
    public void updateByIsbn(String isbn, LendRecord lendRecord) {
        UpdateWrapper<LendRecord> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("isbn", isbn);
        LendRecord lendrecord = new LendRecord();
        lendrecord.setLendTime(lendRecord.getLendTime());
        lendrecord.setReturnTime(lendRecord.getReturnTime());
        lendrecord.setStatus(lendRecord.getStatus());
        lendRecordMapper.update(lendrecord, updateWrapper);
    }

    /**
     * 根据借阅时间更新借阅记录
     * @param lendTime
     * @param lendRecord
     */
    public void updateByLendtime(Date lendTime, LendRecord lendRecord) {
        UpdateWrapper<LendRecord> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("lendTime", lendTime);
        LendRecord lendrecord = new LendRecord();
        lendrecord.setReturnTime(lendRecord.getReturnTime());
        lendrecord.setStatus(lendRecord.getStatus());
        lendRecordMapper.update(lendrecord, updateWrapper);
    }
}
